/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import Client.*;

import Database.AbstractFacade;
import Database.Client;
import Database.ClientFacade;
import Database.Insurance;
import Database.InsuranceFacade;
import constant.PATH;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.lang.*;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;
import java.util.Iterator;
import javax.persistence.EntityManager;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;

/**
 *
 * @author Administrator
 */
/*//////////////////////////////////////////////
 ///////////////////////////////////////////////
    
 post data to client 
    
 ///////////////////////////////////////////////
 //////////////////////////////////////////////*/
public class Server_Client extends UnicastRemoteObject implements Server_Client_Interface {

    @EJB
    private InsuranceFacade insurancefacade;
    @EJB
    private ClientFacade clientfacade;

    @EJB
    private Insurance insurance;
    @EJB
    private Customer currentCustomer;
    @EJB
    private Customer customer;

    private double rate;

    public Server_Client() throws RemoteException {
    }
    

    public static void main(String[] args) {

        String serverName = "MyRMIserver";

        int port = 1099;
        if (args.length == 1) {
            try {
                port = Integer.parseInt(args[0]);
            } catch (NumberFormatException nfe) {
                System.out.println("Port parameter should be an integer. Using default ."+port);
            }
        }
        try {

            
            Server_Client_Interface server = new Server_Client();
//            Naming.rebind("rmi://localhost:" + port + "/" + serverName, server);
            Registry reg = LocateRegistry.createRegistry(port);
            reg.rebind(serverName, server);
            System.out.println("Server started");

        } catch (Exception e) {
           e.printStackTrace();
        }

    }

    @Override
    public boolean login(String id, String password) throws RemoteException {

        System.out.println("Login");
        System.out.println("Id : " + id + "  password : " + password);

        /*
         for (Client cus : clientfacade.findAll()) {
         System.out.println("hi22??");
         if (cus.getId() == id && cus.getPassword() == password) {
         return "true - login success";
         }
         System.out.println("hi66??");
         }*/
        ///  receive the client id and password of who want to login
        //  and return success or fail using clientfacade.....
        return true;
    }

    public boolean sign_up(Customer cus) throws RemoteException {

        System.out.println("Sign up information");
        System.out.println("Insurunce id   : " + cus.getInsurunce_Id());
        System.out.println("Password       : " + cus.getHashSHA256());
        System.out.println("Name           : " + cus.getName());
        System.out.println("Gender         : " + cus.getGender());
        System.out.println("Age            : " + cus.getAge());
        System.out.println("E-mail         : " + cus.getE_mail());
        System.out.println("Insurunce Type : " + cus.getInsurunce_Id());

        
        /*
         System.out.println("before facade !");
        
         client.setAge(cus.getAge());
         client.setEmail(cus.getE_mail());
         client.setGender(cus.getGender());
         client.setId(cus.getInsurunce_Id());
         client.setInsurancetype(cus.getInsurunce_Type());
         client.setPassword(cus.getPassword());
         client.setName(cus.getName());

         clientfacade.create(client);

         System.out.println("after save !");

         */
        // receive the Customer Object . and imply to Client Object 
        // for use ClientFacade DB.
        return true;

    }

    public boolean id_duplicate_check(String insurunce_id) throws RemoteException {

        System.out.println("Client request id_duplicate check");
        System.out.println("Id : " + insurunce_id);

        /*
         for (Client c : clientfacade.findAll()) {
         if (c.getInsurunce_Id() == insurunce_id) {
         return true; // there is id_duplicate!!
         }

         }*/
        // ID_DUPLICATE CHECK USING CLIENT FACADE 
        // CAUSE OF CLIENT IS SAVAED BY CLIENT FACADE DB
        return true; // there is no id_duplicate!!

    }

    public ArrayList<String> toString_Insurunce_information() throws RemoteException {

        ArrayList<String> Insurunce_info = new ArrayList<String>();

        Insurunce_info.add("Type A : Cancer Insurunce    Duration : 20 years    fee : 100,000won    Discount rate depends on age : 5% ");
        Insurunce_info.add("Type B : Life Insurunce      Duration : 30 years    fee :  50,000won    Discount rate depends on age : 3% ");
        Insurunce_info.add("Type C : health Insurunce    Duration : 10 years    fee :  30,000won    Discount rate depends on age : 3% ");
        Insurunce_info.add("Type D : Car Insurunce       Duration : 30 years    fee :  50,000won    Discount rate depends on age : 3% ");

        return Insurunce_info;

        /*    for (Insurance ins : insurancefacade.findAll()) {
         return " [ type=" + ins.getInsurancetype() + " ]\n" + "[ Description=" + ins.getDescription() + " \n]"
         + "[ duration=" + ins.getDuration() + " ]\n" + "[ fee =" + ins.getFee() + " \n]";
         }
         */
        // this function is used for just show the entire insurancd_info 
        // using toString()_function overrided
        // insurance_info include type/fee/duration/description.
    }

    public boolean join(String id, int insurunce_Type) throws RemoteException {

        ClientFacade temp = new ClientFacade();
         /*
         ClientFacade temp = new ClientFacade();
        
         for (Client c : clientfacade.findAll()) {
         if (c.getInsurunce_Id() == id) {
         c.setInsurunce_Type(insurunce_Type);
         return true;
         }

         }*/
        // join changes the insurance_type who logged user. 
        // so clientfacade find the correct client and founded user is changed.

        return false;
    }

    /////////////// no need to rmi function ////////////////////////
    public boolean runExternalAPI() throws RemoteException {
        try {
            ExternalManager.runExternalAPI(PATH.HOMPAGE);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        // In this function , rate is temporary value for fee_rate depends on user age.
        return true;
    }
//////////////////////////////////////////////////////////////////

    public double rate_depends_on_age(Customer c) throws RemoteException {

        if (c.getAge() <= 50) {
            rate = 1.0;
            return c.getAge() * rate;
        } else if (c.getAge() <= 60) {
            rate = 2.0;
            return c.getAge() * rate;
        } else if (c.getAge() > 60) {

            rate = 3.0;

            return c.getAge() * rate;
        } else {
            return 1;
        }
    }

    public ArrayList<String> toString_private_insurunce(Customer currentcustomer) throws RemoteException {

        ArrayList<String> private_info = new ArrayList<String>();

        private_info.add("Cancer Insurunce");
        private_info.add("Duration : 20 years");
        private_info.add("50000");
        private_info.add("5");
         /*
         for (Client cus : clientfacade.findAll()) {
         if (cus.getInsurunce_Id() == currentcustomer.getInsurunce_Id()) {
         return " [ id=" + currentcustomer.getInsurunce_Id() + " ]\n" + "[ password=" + currentcustomer.getPassword() + " \n]"
         + "[ name=" + currentcustomer.getName() + " ]\n" + "[ age=" + currentcustomer.getAge() + " \n]"
         + "[ gender=" + currentcustomer.getGender() + " \n]" + "[ e_mail=" + currentcustomer.getE_mail() + " ]\n" + "[ Insuru_Type=" + currentcustomer.getInsurunce_Type() + " \n]"
         + "[ insure_type=" + currentcustomer.getInsurunce_Type() + " \n]";
         }

         }*/ 
       // this function is used for just show the correct user insurancd_info 
        // using toString()_function overrided
        // user_private_insurance_info include user_private_info
        // user's private insurance_info that include user's insurance_type , insurance_fee, insurance_type;
        //return "sorry! - access failed";
        return private_info;
    }
}
